//
//  WJUserAccountViewModel.swift
//  WJWeiBo
//
//  Created by wangjun on 16/7/22.
//  Copyright © 2016年 wangjun. All rights reserved.
//

import UIKit

class WJUserAccountViewModel: NSObject {

    
    let file = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    
    
    
    static let shareTools:WJUserAccountViewModel = WJUserAccountViewModel()
    
    var userAccoun: WJUserAccount?
    
    var accessToken:String?{
        
        if userAccoun?.access_token != nil {
  
            if userAccoun?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
                
                return userAccoun?.access_token
                
            }else {

                return nil
            }
        }else {

            return nil
        }
    }
    
    // 判断用户是否登录
    // 通过accessToken 是否为nil  为nil 为未登录
    var isLogin: Bool {
        
        return accessToken != nil
    }


    override init() {
        super.init()
        userAccoun = getUserAccont()
    }
    
}



extension WJUserAccountViewModel {
//保存对象
    func saveUserAccount(userAccount:WJUserAccount){
        
        self.userAccoun = userAccount
        
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: file)
        
    }
    
    //获取对象
    func getUserAccont()->WJUserAccount? {
 
        // 获取对象
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(file) as? WJUserAccount
        
        return result
    }
}

extension WJUserAccountViewModel {
    
    func loadUserAccount(code: String, isSuccess:(isSucc: Bool)->()) {
        
            WJNetworkingTools.shareNetworkingTools.loadUserAcount(code, success: { (response) -> () in
                guard let res = response as? [String:AnyObject] else {
                    isSuccess(isSucc: false)
                    
                    return
                }
                
                let userAccount: WJUserAccount = WJUserAccount(dict: res)
                
                self.getUserInfo(userAccount,sucess:isSuccess)
                
                }, failure: { (error) -> () in
                    print("请求失败\(error)")
                    
                    isSuccess(isSucc: false)
            })
        
        }

    //获取用户信息数据
    func getUserInfo(userAccount:WJUserAccount,sucess: (isSucc: Bool)->()){
        
        let userInfoUrl = "https://api.weibo.com/2/users/show.json"
        
        let param = [
            "access_token":userAccount.access_token!,
            "uid":userAccount.uid!
        ]
        
        WJNetworkingTools.shareNetworkingTools.GET(userInfoUrl, parameters: param, progress: nil, success: { (_, response) -> Void in
            
            guard let res = response as? [String :AnyObject]  else {
                
                return
            }
            
            //根据传过来的模型来赋值
            userAccount.avatar_large = res["avatar_large"] as? String
            userAccount.screen_name = res["screen_name"] as? String
            self.saveUserAccount(userAccount)
            
            sucess(isSucc: true)
            
            }) { (_, error) -> Void in
                
                print(error)
        }
        
    }

}



